package com.jiangman.weather.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.jiangman.weather.BaseApplication;
import com.jiangman.weather.R;
import com.jiangman.weather.base.BaseFragment;
import com.jiangman.weather.bean.ConstantMsg;
import com.jiangman.weather.bean.LoginResponse;
import com.jiangman.weather.bean.MessageEvent;
import com.jiangman.weather.callback.CustomCallback;
import com.jiangman.weather.callback.RsponseStateListenerImp;
import com.jiangman.weather.constant.ConstantSp;
import com.jiangman.weather.constant.ConstantUrl;
import com.jiangman.weather.view.EditTextWithDel;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import util.SPUtils;
import util.ToastUitl;

/**
 * Created by secoro on 2017/4/27.
 */

public class LoginFragment extends BaseFragment {
    @BindView(R.id.userph)
    EditTextWithDel userph;
    @BindView(R.id.userpass)
    EditTextWithDel userpass;
    @BindView(R.id.bt_login)
    Button btLogin;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_login, null);
        ButterKnife.bind(this, inflate);
        return inflate;
    }

    @OnClick(R.id.bt_login)
    public void onClick() {

        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl+"Weather/login")
                .addParams("user_name",userph.getText().toString())
                .addParams("user_pwd",userpass.getText().toString())
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<LoginResponse>() {

                    @Override
                    public void fail(LoginResponse response) {


                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);

                    }

                    @Override
                    public void success(LoginResponse response, boolean isRefresh) {
                        LoginResponse.DataBean data = response.data;
                        ConstantSp.USER_ID=String.valueOf(data.id);
                        ConstantSp.USER_NAME=data.username;
                        MessageEvent messageEvent = new MessageEvent();
                        messageEvent.user_name=ConstantSp.USER_NAME;
                        messageEvent.message_id= ConstantMsg.update_user;
                        EventBus.getDefault().post(messageEvent);
                        MessageEvent messageEvent2 = new MessageEvent();
                        messageEvent2.user_name=ConstantSp.USER_NAME;
                        messageEvent2.message_id= ConstantMsg.login_finish;
                        EventBus.getDefault().post(messageEvent);
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);

                        BaseApplication.isLogin=true;


                        SPUtils.setSharedStringData(getActivity(),"userName",data.username);
                        SPUtils.setSharedStringData(getActivity(),"userPwd",String.valueOf(data.pwd));

                    }

                    @Override
                    public void error(Exception e) {
                        System.out.println(e.toString());
                    }
                }, LoginResponse.class, false));
    }


}
